import time

import false_email
import multithreading_spider


class SpiderMain(object):
    def __init__(self):
        self.f_email = false_email.FalseEmail()
        self.threads = multithreading_spider.Theads()

    def main(self):
        while True:
            past_time = int(time.strftime('%H', time.localtime()))
            print(past_time)

            start_title = "开始采集时间"
            # now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
            start_time = str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
            self.f_email.send_email(start_title, start_time)

            if past_time == 23:
                past_time = -1

            xm_records, zq_records, dy_records = self.threads.theads()
            last_title = '采集完成'
            count = str(xm_records + zq_records + dy_records)
            last_time = str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
            content = '完成时间：{0}。 共采集：战旗：{1}条， 熊猫：{2}条， 斗鱼：{3}条,  共{4}条'.format(last_time, zq_records, xm_records, dy_records,
                                                                         count)
            self.f_email.send_email(last_title, content)

            while (int(time.strftime('%H', time.localtime())) != past_time + 1):
                time.sleep(300)


if __name__ == '__main__':
    spider = SpiderMain()
    spider.main()

    # while True:
    #     past_time = int(time.strftime('%H', time.localtime()))
    #     print(past_time)
    #     if past_time == 23:
    #         past_time = -1
    #     content = "当前状态"
    #     name = str(past_time)
    #     false_email.FalseEmail().send_email(content, name)
    #
    #     zhanqi_main.SpiderMain().craw()
    #     panda_main.SpiderMain().craw()
    #     douyu_main.SpiderMain().craw()
    #
    #     while (int(time.strftime('%H', time.localtime())) != past_time + 1):
    #         time.sleep(600)
